﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Terraria.ModLoader.Exceptions
{
	// Token: 0x020002F2 RID: 754
	internal static class LevenshteinDistance
	{
		// Token: 0x06001D12 RID: 7442 RVA: 0x00437FF8 File Offset: 0x004361F8
		internal static string FolderAwareEditDistance(string source, string[] targets)
		{
			if (targets.Length == 0)
			{
				return null;
			}
			char separator = '/';
			string[] source2 = source.Split(new char[]
			{
				separator
			});
			List<string> sourceFolders = source2.Reverse<string>().Skip(1).ToList<string>();
			string sourceFile = source2.Last<string>();
			int missingFolderPenalty = 4;
			int extraFolderPenalty = 3;
			var source3 = targets.Select(delegate(string target)
			{
				string[] source4 = target.Split(new char[]
				{
					separator
				});
				List<string> targetFolders = source4.Reverse<string>().Skip(1).ToList<string>();
				string s = source4.Last<string>();
				IEnumerable<string> second = from x in sourceFolders
				where targetFolders.Contains(x)
				select x;
				List<string> list = sourceFolders.Except(second).ToList<string>();
				List<string> list2 = targetFolders.Except(second).ToList<string>();
				int num = 0;
				int num2 = list.Count - list2.Count;
				if (num2 > 0)
				{
					num += num2 * missingFolderPenalty;
				}
				else if (num2 < 0)
				{
					num += -num2 * extraFolderPenalty;
				}
				if (list.Count > 0 && list.Count >= list2.Count)
				{
					using (List<string>.Enumerator enumerator = list2.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							string s2 = enumerator.Current;
							int num3 = int.MaxValue;
							foreach (string t in list)
							{
								num3 = Math.Min(num3, LevenshteinDistance.Compute(s2, t));
							}
							num += num3;
						}
						goto IL_1D3;
					}
				}
				if (list.Count > 0)
				{
					foreach (string s3 in list)
					{
						int num4 = int.MaxValue;
						foreach (string t2 in list2)
						{
							num4 = Math.Min(num4, LevenshteinDistance.Compute(s3, t2));
						}
						num += num4;
					}
				}
				IL_1D3:
				num += LevenshteinDistance.Compute(s, sourceFile);
				return new
				{
					Target = target,
					Score = num
				};
			});
			var orderedEnumerable = from x in source3
			orderby x.Score
			select x;
			return (from x in source3
			orderby x.Score
			select x).First().Target;
		}

		// Token: 0x06001D13 RID: 7443 RVA: 0x004380C8 File Offset: 0x004362C8
		public static int Compute(string s, string t)
		{
			int length = s.Length;
			int length2 = t.Length;
			int[,] array = new int[length + 1, length2 + 1];
			if (length == 0)
			{
				return length2;
			}
			if (length2 == 0)
			{
				return length;
			}
			int i = 0;
			while (i <= length)
			{
				array[i, 0] = i++;
			}
			int j = 0;
			while (j <= length2)
			{
				array[0, j] = j++;
			}
			for (int k = 1; k <= length; k++)
			{
				for (int l = 1; l <= length2; l++)
				{
					int num = (t[l - 1] == s[k - 1]) ? 0 : 2;
					array[k, l] = Math.Min(Math.Min(array[k - 1, l] + 2, array[k, l - 1] + 2), array[k - 1, l - 1] + num);
				}
			}
			return array[length, length2];
		}
	}
}
